package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPurchasingunit;
import com.ruoyi.system.service.ISysPurchasingunitService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 采购单位Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/purchasingunit")
public class SysPurchasingunitController extends BaseController
{
    private String prefix = "system/purchasingunit";
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysPurchasingunitService sysPurchasingunitService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:purchasingunit:view")
    @GetMapping()
    public String purchasingunit()
    {
        return prefix + "/purchasingunit";
    }

    /**
     * 查询采购单位列表
     */
    @RequiresPermissions("system:purchasingunit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysPurchasingunit sysPurchasingunit)
    {
        startPage();
        List<SysPurchasingunit> list = sysPurchasingunitService.selectSysPurchasingunitList(sysPurchasingunit);
        return getDataTable(list);
    }
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:purchasingunit:list")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysPurchasingunit> util = new ExcelUtil<SysPurchasingunit>(SysPurchasingunit.class);
        List<SysPurchasingunit> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = sysPurchasingunitService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:purchasingunit:list")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysPurchasingunit> util = new ExcelUtil<SysPurchasingunit>(SysPurchasingunit.class);
        return util.importTemplateExcel("用户数据");
    }
    /**
     * 导出采购单位列表
     */
    @RequiresPermissions("system:purchasingunit:export")
    @Log(title = "采购单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysPurchasingunit sysPurchasingunit)
    {
        List<SysPurchasingunit> list = sysPurchasingunitService.selectSysPurchasingunitList(sysPurchasingunit);
        ExcelUtil<SysPurchasingunit> util = new ExcelUtil<SysPurchasingunit>(SysPurchasingunit.class);
        return util.exportExcel(list, "purchasingunit");
    }

    /**
     * 新增采购单位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存采购单位
     */
    @RequiresPermissions("system:purchasingunit:add")
    @Log(title = "采购单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysPurchasingunit sysPurchasingunit)
    {

        sysPurchasingunit.setSalt(ShiroUtils.randomSalt());
        sysPurchasingunit.setPassword(passwordService.encryptPassword(sysPurchasingunit.getAccount(), sysPurchasingunit.getPassword(), sysPurchasingunit.getSalt()));
        sysPurchasingunit.setCreateBy(ShiroUtils.getLoginName());
    int sysId = sysPurchasingunitService.insertSysPurchasingunit(sysPurchasingunit);
        SysUser sysUser = new SysUser();
        sysUser.setSalt(sysPurchasingunit.getSalt());
        sysUser.setPassword(sysPurchasingunit.getPassword());
        sysUser.setCreateBy(ShiroUtils.getLoginName());
        sysUser.setLoginName(sysPurchasingunit.getAccount());
        sysUser.setUserName(sysPurchasingunit.getUnitName());
        sysUser.setUserType("10");//10表示采购单位
        sysUser.setCompanyId(sysPurchasingunit.getPurchasingUnitId());

        return toAjax(userService.insertUser(sysUser));
    }

    /**
     * 修改采购单位
     */
    @GetMapping("/edit/{purchasingUnitId}")
    public String edit(@PathVariable("purchasingUnitId") Long purchasingUnitId, ModelMap mmap)
    {
        SysPurchasingunit sysPurchasingunit = sysPurchasingunitService.selectSysPurchasingunitById(purchasingUnitId);
        mmap.put("sysPurchasingunit", sysPurchasingunit);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购单位
     */
    @RequiresPermissions("system:purchasingunit:edit")
    @Log(title = "采购单位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysPurchasingunit sysPurchasingunit)
    {
        return toAjax(sysPurchasingunitService.updateSysPurchasingunit(sysPurchasingunit));
    }

    /**
     * 删除采购单位
     */
    @RequiresPermissions("system:purchasingunit:remove")
    @Log(title = "采购单位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysPurchasingunitService.deleteSysPurchasingunitByIds(ids));
    }
    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUser user)
    {
        return userService.checkLoginNameUnique(user.getLoginName());
    }

    /**
     * 重置密码界面
     * @param purchasingUnitId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:purchasingunit:resetPwd")
    @GetMapping("/resetPwd/{purchasingUnitId}")
    public String resetPwd(@PathVariable("purchasingUnitId") Long purchasingUnitId, ModelMap mmap)
    {
        mmap.put("purchasingUnit", sysPurchasingunitService.selectSysPurchasingunitById(purchasingUnitId));
        return prefix + "/resetPwd";
    }

    /**
     * 采购单位重置密码
     * @param sysPurchasingunit
     * @return
     */
    @RequiresPermissions("system:purchasingunit:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysPurchasingunit sysPurchasingunit)
    {

        sysPurchasingunit.setSalt(ShiroUtils.randomSalt());
        sysPurchasingunit.setPassword(passwordService.encryptPassword(sysPurchasingunit.getAccount(), sysPurchasingunit.getPassword(), sysPurchasingunit.getSalt()));
        if (sysPurchasingunitService.resetUserPwd(sysPurchasingunit) > 0)
        {

            return success();
        }
        return error();
    }
}
